<template>
  <div class="page-container animate-fade-in">
    <div class="page-header">
      <h1 class="gradient-text">银行卡归属地查询</h1>
      <p>支持多种查询方式，快速获取银行卡信息</p>
    </div>

    <el-row :gutter="24">
      <el-col :span="12">
        <div class="card-container">
          <h3>
            <el-icon><CreditCard /></el-icon>
            V1.0 查询接口
          </h3>
          <p class="api-desc">使用GET请求方式，简单快捷</p>
          
          <el-form @submit.prevent="queryV1" class="query-form">
            <el-form-item label="银行卡号">
              <el-input
                v-model="v1Form.cardNumber"
                placeholder="请输入银行卡号"
                clearable
                :disabled="v1Loading"
              >
                <template #prefix>
                  <el-icon><CreditCard /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                @click="queryV1"
                :loading="v1Loading"
                class="query-btn"
              >
                <el-icon><Search /></el-icon>
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>

      <el-col :span="12">
        <div class="card-container">
          <h3>
            <el-icon><Lock /></el-icon>
            V2.0 查询接口
          </h3>
          <p class="api-desc">使用POST请求，更加安全</p>
          
          <el-form @submit.prevent="queryV2" class="query-form">
            <el-form-item label="银行卡号">
              <el-input
                v-model="v2Form.cardNumber"
                placeholder="请输入银行卡号"
                clearable
                :disabled="v2Loading"
              >
                <template #prefix>
                  <el-icon><CreditCard /></el-icon>
                </template>
              </el-input>
            </el-form-item>
            
            <el-form-item>
              <el-button
                type="primary"
                @click="queryV2"
                :loading="v2Loading"
                class="query-btn"
              >
                <el-icon><Search /></el-icon>
                查询
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>

    <!-- 查询结果 -->
    <div v-if="queryResult" class="card-container result-card">
      <h3>
        <el-icon><DocumentChecked /></el-icon>
        查询结果
      </h3>
      
      <el-descriptions :column="2" border>
        <el-descriptions-item label="银行名称">
          {{ queryResult.bankName || '未知' }}
        </el-descriptions-item>
        <el-descriptions-item label="卡类型">
          {{ queryResult.cardType || '未知' }}
        </el-descriptions-item>
        <el-descriptions-item label="发卡行">
          {{ queryResult.issuer || '未知' }}
        </el-descriptions-item>
        <el-descriptions-item label="归属地">
          {{ queryResult.location || '未知' }}
        </el-descriptions-item>
      </el-descriptions>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { bankApi } from '@/api/bank'
import { validateBankCard, detectSqlInjection, sanitizeInput } from '@/utils/validator'

const v1Form = ref({
  cardNumber: ''
})

const v2Form = ref({
  cardNumber: ''
})

const v1Loading = ref(false)
const v2Loading = ref(false)
const queryResult = ref(null)

const queryV1 = async () => {
  const cardNumber = sanitizeInput(v1Form.value.cardNumber)
  
  // 前端验证
  if (!cardNumber) {
    ElMessage.warning('请输入银行卡号')
    return
  }
  
  // 检测恶意输入
  if (detectSqlInjection(cardNumber)) {
    ElMessage.error('输入包含非法字符')
    return
  }
  
  // 银行卡号格式验证
  const validation = validateBankCard(cardNumber)
  if (!validation.valid) {
    ElMessage.error(validation.message)
    return
  }
  
  v1Loading.value = true
  try {
    const response = await bankApi.queryV1(cardNumber)
    if (response.code === 200) {
      queryResult.value = response.data
      ElMessage.success('查询成功')
    } else {
      ElMessage.error(response.message || '查询失败')
    }
  } catch (error) {
    ElMessage.error('查询失败：' + error.message)
  } finally {
    v1Loading.value = false
  }
}

const queryV2 = async () => {
  const cardNumber = sanitizeInput(v2Form.value.cardNumber)
  
  // 前端验证
  if (!cardNumber) {
    ElMessage.warning('请输入银行卡号')
    return
  }
  
  // 检测恶意输入
  if (detectSqlInjection(cardNumber)) {
    ElMessage.error('输入包含非法字符')
    return
  }
  
  // 银行卡号格式验证
  const validation = validateBankCard(cardNumber)
  if (!validation.valid) {
    ElMessage.error(validation.message)
    return
  }
  
  v2Loading.value = true
  try {
    const response = await bankApi.queryV2({ card: cardNumber })
    if (response.code === 200) {
      queryResult.value = response.data
      ElMessage.success('查询成功')
    } else {
      ElMessage.error(response.message || '查询失败')
    }
  } catch (error) {
    ElMessage.error('查询失败：' + error.message)
  } finally {
    v2Loading.value = false
  }
}
</script>

<style scoped lang="scss">
.page-header {
  text-align: center;
  margin-bottom: 40px;
  
  h1 {
    font-size: 2rem;
    margin-bottom: 8px;
  }
  
  p {
    color: rgba(255, 255, 255, 0.8);
    font-size: 1.1rem;
  }
}

.card-container {
  h3 {
    display: flex;
    align-items: center;
    margin-bottom: 16px;
    color: #333;
    
    .el-icon {
      margin-right: 8px;
    }
  }
  
  .api-desc {
    color: #666;
    margin-bottom: 24px;
  }
}

.query-form {
  .query-btn {
    width: 100%;
    height: 40px;
    font-size: 16px;
  }
}

.result-card {
  margin-top: 24px;
  animation: fadeInUp 0.5s ease-out;
  
  h3 {
    color: #67C23A;
    margin-bottom: 20px;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(30px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>